package com.choudou5.solr.util;

import cn.hutool.core.codec.Base64;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;

/**
 * @Name：GzipUtil 说明
 * @@author choudou5
 * @@date 2018-03-04
 */
public class GzipUtil {

    public static void main(String[] args) throws IOException {
        String str = "我是中国人";
        System.out.println("原长度：" + str.length());

        String compress = GzipUtil.compress(str);
        System.out.println("压缩后长度：" + compress.length());
        System.out.println("压缩后内容：" + compress);


        System.out.println("解压后内容：" + GzipUtil.uncompress(compress));
    }

    /**
     * 压缩
     * @param str
     */
    public static String compress(String str) {
        if (StrUtil.isEmpty(str))
            return str;
        ByteArrayOutputStream out = null;
        GZIPOutputStream gzip = null;
        String compress = "";
        try {
            out = new ByteArrayOutputStream();
            gzip = new GZIPOutputStream(out);
            gzip.write(str.getBytes());
            gzip.close();
            // 这里增加base64编码
            compress = Base64.encode(out.toByteArray());
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (null != out) {
                try {
                    out.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return compress;
    }

    /**
     * 解压缩
     * @param str
     */
    public static String uncompress(String str) {
        if (StrUtil.isEmpty(str))
            return str;
        ByteArrayOutputStream out = null;
        ByteArrayInputStream in = null;
        GZIPInputStream gzip = null;
        String uncompress = "";
        try {
            out = new ByteArrayOutputStream();
            // 这里增加base64解码
            in = new ByteArrayInputStream(Base64.decode(str));
            gzip = new GZIPInputStream(in);
            byte[] buffer = new byte[1024];
            int offset = -1;
            while ((offset = gzip.read(buffer)) != -1) {
                out.write(buffer, 0, offset);
            }
            uncompress = out.toString();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (null != gzip) {
                try {
                    gzip.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (null != in) {
                try {
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (null != out) {
                try {
                    out.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return uncompress;
    }

}
